package com.artisan.view;

import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.artisan.model.UserType;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class MainFrm extends JFrame {

	private JPanel contentPane;
	private JDesktopPane desktopPane;
	public static UserType userType;
	public static Object userObject;
	private JMenuItem addStudentMenuItem;
	private JMenu manageClassMenu;
	private JMenu manageTeacherMenu;
	private JMenuItem addTeacherMenuItem;
	private JMenu courseMenu;
	private JMenuItem addScoreMenuItem;
	private JMenuItem viewScoreMenuItem;
	private JMenuItem manageScoreMenuItem;
	private JMenu selectCourseMenu;

	/**
	 * Create the frame.
	 */
	public MainFrm(UserType userType, Object userObject) {
		setExtendedState(Frame.MAXIMIZED_BOTH);
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrm.class.getResource("/com/artisan/image/学生管理.png")));
		setFont(new Font("微软雅黑", Font.PLAIN, 14));
		this.userType = userType;
		this.userObject = userObject;
		setTitle("学生信息管理界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1050, 680);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		// 保证主界面不会覆盖电脑屏幕的任务栏
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(new JFrame().getGraphicsConfiguration());
		Rectangle rectangle = new Rectangle(screenInsets.left, screenInsets.top,
				screenSize.width - screenInsets.left - screenInsets.right,
				screenSize.height - screenInsets.top - screenInsets.bottom);
		// 根据屏幕大小设置主界面大小
		setBounds(rectangle);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("系统设置");
		mnNewMenu.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mnNewMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/系统设置.png")));
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("修改密码");
		mntmNewMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				editPassword(ae);
			}
		});
		mntmNewMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/修改密码.png")));
		mnNewMenu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("退出系统");
		mntmNewMenuItem_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(MainFrm.this, "您确认退出吗?", "退出提示", 0) == JOptionPane.OK_OPTION) {
					MainFrm.this.dispose();
					new LoginFrm().setVisible(true);
				}
			}
		});
		mntmNewMenuItem_1.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/退出.png")));
		mnNewMenu.add(mntmNewMenuItem_1);

		JMenu mnNewMenu_1 = new JMenu("学生管理");
		mnNewMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mnNewMenu_1.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/学生管理.png")));
		menuBar.add(mnNewMenu_1);

		addStudentMenuItem = new JMenuItem("学生添加");
		addStudentMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		addStudentMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddStudentFrm addStudentFrm = new AddStudentFrm();
				addStudentFrm.setVisible(true);
				desktopPane.add(addStudentFrm);
			}
		});
		addStudentMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/添加.png")));
		mnNewMenu_1.add(addStudentMenuItem);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("学生列表");
		mntmNewMenuItem_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManageStudentFrm studentManageFrm = new ManageStudentFrm();
				studentManageFrm.setVisible(true);
				desktopPane.add(studentManageFrm);
			}
		});
		mntmNewMenuItem_3.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/用户列表.png")));
		mnNewMenu_1.add(mntmNewMenuItem_3);

		manageClassMenu = new JMenu("班级管理");
		manageClassMenu.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		manageClassMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/班级管理.png")));
		menuBar.add(manageClassMenu);

		JMenuItem mntmNewMenuItem_4 = new JMenuItem("班级添加");
		mntmNewMenuItem_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				addStudentClass(ae);
			}
		});
		mntmNewMenuItem_4.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/添加.png")));
		manageClassMenu.add(mntmNewMenuItem_4);

		JMenuItem mntmNewMenuItem_5 = new JMenuItem("班级管理");
		mntmNewMenuItem_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManageClassFrm classManageFrm = new ManageClassFrm();
				classManageFrm.setVisible(true);
				desktopPane.add(classManageFrm);
			}
		});
		mntmNewMenuItem_5.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/班级列表.png")));
		manageClassMenu.add(mntmNewMenuItem_5);

		manageTeacherMenu = new JMenu("教师管理");
		manageTeacherMenu.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		manageTeacherMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/老师.png")));
		menuBar.add(manageTeacherMenu);

		addTeacherMenuItem = new JMenuItem("添加教师");
		addTeacherMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		addTeacherMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddTeacherFrm addTeacherFrm = new AddTeacherFrm();
				addTeacherFrm.setVisible(true);
				desktopPane.add(addTeacherFrm);
			}
		});
		addTeacherMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/添加.png")));
		manageTeacherMenu.add(addTeacherMenuItem);

		JMenuItem mntmNewMenuItem_8 = new JMenuItem("教师列表");
		mntmNewMenuItem_8.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManageTeacherFrm manageTeacherFrm = new ManageTeacherFrm();
				manageTeacherFrm.setVisible(true);
				desktopPane.add(manageTeacherFrm);
			}
		});
		mntmNewMenuItem_8.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/用户列表.png")));
		manageTeacherMenu.add(mntmNewMenuItem_8);

		courseMenu = new JMenu("课程管理");
		courseMenu.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		courseMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/课程.png")));
		menuBar.add(courseMenu);

		JMenuItem addCourseMenuItem = new JMenuItem("添加课程");
		addCourseMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		addCourseMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddCourseFrm addCourseFrm = new AddCourseFrm();
				addCourseFrm.setVisible(true);
				desktopPane.add(addCourseFrm);
			}
		});
		addCourseMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/添加.png")));
		courseMenu.add(addCourseMenuItem);

		JMenuItem courseListMenuItem = new JMenuItem("课程列表");
		courseListMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		courseListMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManageCourseFrm manageCourseFrm = new ManageCourseFrm();
				manageCourseFrm.setVisible(true);
				desktopPane.add(manageCourseFrm);
			}
		});
		courseListMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/课程列表.png")));
		courseMenu.add(courseListMenuItem);

		selectCourseMenu = new JMenu("选课管理");
		selectCourseMenu.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		selectCourseMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/选择.png")));
		menuBar.add(selectCourseMenu);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("选课管理");
		mntmNewMenuItem_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManangeSelectedCourseFrm manangeSelectedCourseFrm = new ManangeSelectedCourseFrm();
				manangeSelectedCourseFrm.setVisible(true);
				desktopPane.add(manangeSelectedCourseFrm);
			}
		});
		mntmNewMenuItem_2.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/选择.png")));
		selectCourseMenu.add(mntmNewMenuItem_2);

		JMenu mnNewMenu_2 = new JMenu("成绩管理");
		mnNewMenu_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mnNewMenu_2.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/成绩.png")));
		menuBar.add(mnNewMenu_2);

		addScoreMenuItem = new JMenuItem("录入成绩");
		addScoreMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddScoreFrm addScoreFrm = new AddScoreFrm();
				addScoreFrm.setVisible(true);
				desktopPane.add(addScoreFrm);
			}
		});
		addScoreMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		addScoreMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/添加.png")));
		mnNewMenu_2.add(addScoreMenuItem);

		viewScoreMenuItem = new JMenuItem("成绩查看");
		viewScoreMenuItem.setEnabled(false);
		viewScoreMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				ViewscoreFrm viewscoreFrm = new ViewscoreFrm();
				viewscoreFrm.setVisible(true);
				desktopPane.add(viewscoreFrm);
			}
		});
		viewScoreMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		viewScoreMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/查看.png")));
		mnNewMenu_2.add(viewScoreMenuItem);

		manageScoreMenuItem = new JMenuItem("成绩管理");
		manageScoreMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				ManageScoreFrm manageScoreFrm = new ManageScoreFrm();
				manageScoreFrm.setVisible(true);
				desktopPane.add(manageScoreFrm);
			}
		});
		manageScoreMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/成绩.png")));
		manageScoreMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mnNewMenu_2.add(manageScoreMenuItem);

		JMenu mnNewMenu_3 = new JMenu("帮助");
		mnNewMenu_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mnNewMenu_3.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/帮助.png")));
		menuBar.add(mnNewMenu_3);

		JMenuItem mntmNewMenuItem_6 = new JMenuItem("关于我们");
		mntmNewMenuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				aboutUs(ae);
			}
		});
		mntmNewMenuItem_6.setIcon(new ImageIcon(MainFrm.class.getResource("/com/artisan/image/关于我们.png")));
		mnNewMenu_3.add(mntmNewMenuItem_6);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);

		desktopPane = new JDesktopPane();
		desktopPane.setBackground(new Color(144, 238, 144));
		GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
		gl_desktopPane.setHorizontalGroup(
			gl_desktopPane.createParallelGroup(Alignment.LEADING)
				.addGap(0, 1606, Short.MAX_VALUE)
		);
		gl_desktopPane.setVerticalGroup(
			gl_desktopPane.createParallelGroup(Alignment.LEADING)
				.addGap(0, 774, Short.MAX_VALUE)
		);
		desktopPane.setLayout(gl_desktopPane);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 1512, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
		);
		contentPane.setLayout(gl_contentPane);
		setLocationRelativeTo(null);
		setAuthority();
	}

	protected void addStudentClass(ActionEvent ae) {
		// TODO 自动生成的方法存根
		AddStudentClassFrm sca = new AddStudentClassFrm();
		sca.setVisible(true);
		desktopPane.add(sca);
	}

	protected void editPassword(ActionEvent ae) {
		// TODO 自动生成的方法存根
		EditPasswordFrm editPasswordFrm = new EditPasswordFrm();
		editPasswordFrm.setVisible(true);
		desktopPane.add(editPasswordFrm);
	}

	protected void aboutUs(ActionEvent ae) {
		// TODO 自动生成的方法存根
		String info = "                 【段奇乐】出品\n";
		info += "很高兴你能使用我做的系统，欢迎您的加入!\n";
		info += "        小船静静往返，马蒂斯的海岸\n";
		info += "        星空下的夜晚，交给梵谷点燃\n";
		info += "        梦美的太短暂，孟克桥上呐喊\n";
		info += "        这世上的热闹，出自孤单......\n";
		JOptionPane.showMessageDialog(this, info, "关于我们", -1);
	}

	private void setAuthority() {
		if ("学生".equals(userType.getName())) {
			addStudentMenuItem.setEnabled(false);
			manageClassMenu.setEnabled(false);
			manageTeacherMenu.setEnabled(false);
			courseMenu.setEnabled(false);
			addScoreMenuItem.setEnabled(false);
			viewScoreMenuItem.setEnabled(true);
			manageScoreMenuItem.setEnabled(false);
		}
		if ("教师".equals(userType.getName())) {
			addTeacherMenuItem.setEnabled(false);
			selectCourseMenu.setEnabled(false);
		}
	}
}
